package mn.more.mock.io.xml;

import java.io.File;
import java.io.IOException;
import java.util.List;
import junit.framework.TestCase;
import mn.more.foundation.io.FileOperator;
import mn.more.mock.io.MockDataReader;
import mn.more.mock.io.MockModel;

/**
 * Created by IntelliJ IDEA.
 * User: Buyaa
 * Date: Jun 25, 2008
 * Time: 2:06:18 PM
 * To change this template use File | Settings | File Templates.
 */
public class WriterTestArray extends TestCase {

	private static String filename = "/mock_model_test_drive.xml";
	private static String fileLocation;
	private MockModel user1;
	private MockModel person1;
	private static final String KEY_1 = "myStringArray";
	private static final String BYE = "bye";
	private String[] strArr;
	private static final String USER1 = "user1";
	private static final String PERSON1 = "person1";
	private static final String USER_ID = "userId";
	private double[] dblArr;
	char[] charArr;
	private static final String DOUBLE_ARRAY = "myDoubleArray";
	private static final String AGE = "age";
	private static final String PROFILE = "profile";
	private static final String PERSON1_NAME = "person1_name";
	private static final String NAME = "name";
	private static final String PASSWORD = "password";
	private static final String PASSWORD_1 = "password_1";
	private static final String USER_ID_1 = "userId_1";
	private static final String CHAR_ARRAY = "myCharArray";

	protected void setUp() throws Exception {
		fileLocation = getClass().getResource("/").getFile() + filename;
		fileLocation = fileLocation.replaceAll("%20", " ");
		user1 = new MockModel(USER1);
		strArr = new String[]{
			"hello", "nh\ni",
			BYE, "goodbye"
		};
		dblArr = new double[]{-24545.2, 451245.5656, 0.589};
		int[] intArr = new int[]{2, 1245, 467};
		float[] floatArr = new float[]{2.2f, 1245.565f, 467.589f};
		charArr = new char[]{'m', '1', 'g'};//(char)9};
		user1.setValue(USER_ID, USER_ID_1);
		user1.setValue(PASSWORD, PASSWORD_1);
		person1 = new MockModel(PERSON1);
		person1.setValue(NAME, PERSON1_NAME);
		person1.setValue(AGE, 25);
		person1.setValue(PROFILE, user1);
		person1.setValue(KEY_1, strArr);
		person1.setValue(DOUBLE_ARRAY, dblArr);
		person1.setValue(CHAR_ARRAY, charArr);
		person1.setValue("myIntArray", intArr);
		person1.setValue("myFloatArray", floatArr);
		super.setUp();
	}

	protected void tearDown() throws Exception {
		FileOperator.delete(fileLocation);
		super.tearDown();
	}

	public void testWriteAndRead() throws Exception {
		// test the writing process
		XMLWriter writer = new XMLWriter();
		try {
			writer.setFilename(filename, true);
			writer.write(person1);
		} catch (IOException e) {
			fail("failed at write(): " + e.getMessage());
		}

		// make sure that the file is really there
		File f = new File(fileLocation);
		assertNotNull(f);
		assertTrue(f.isFile());
		assertTrue(f.canRead());

		// test the reading process
		MockDataReader reader = new XMLReader();
		try {
			reader.setFilename(filename, true);
			reader.read();
		} catch (IOException e) {
			fail("failed at read(): " + e.getMessage());
		}

		List<String> ids = reader.getIdList();
		assertNotNull(ids);
		assertEquals(ids.size(), 2);
		assertTrue(ids.contains(USER1));
		assertTrue(ids.contains(PERSON1));

		MockModel _user1 = reader.getModel(USER1);
		assertNotNull(_user1);
		assertEquals(_user1.getString(USER_ID), USER_ID_1);
		assertEquals(_user1.getString(PASSWORD), PASSWORD_1);

		MockModel _person1 = reader.getModel(PERSON1);
		assertNotNull(_person1);
		assertEquals(_person1.getString(NAME), PERSON1_NAME);
		assertEquals(_person1.getInt(AGE), 25);
		assertEquals(_person1.getRefModel(PROFILE), _user1);
		assertTrue(_person1.getStringArray(KEY_1).length == strArr.length);
		assertTrue(_person1.getStringArray(KEY_1)[2].equals(BYE));
		assertTrue(_person1.getDoubleArray(DOUBLE_ARRAY).length == dblArr.length);
		assertTrue(_person1.getDoubleArray(DOUBLE_ARRAY)[2] == dblArr[2]);
		assertTrue(_person1.getDoubleArray(DOUBLE_ARRAY)[1] == dblArr[1]);
		assertTrue(_person1.getDoubleArray(DOUBLE_ARRAY)[0] == dblArr[0]);
		assertTrue(_person1.getCharArray(CHAR_ARRAY).length == charArr.length);
		assertTrue(_person1.getCharArray(CHAR_ARRAY)[2] == charArr[2]);
		assertTrue(_person1.getCharArray(CHAR_ARRAY)[1] == charArr[1]);
		assertTrue(_person1.getCharArray(CHAR_ARRAY)[0] == charArr[0]);
	}
}

